/**
 * @author Isabella Nake (isabella.nake@usi.ch)
 * @author Nicolas Lusa (lusan@usi.ch)
 */
package ch.usi.inf.pf2.gui.tool;

import java.awt.event.MouseEvent;

import ch.usi.inf.pf2.circuit.Component;
import ch.usi.inf.pf2.circuit.Input;
import ch.usi.inf.pf2.circuit.Value;
import ch.usi.inf.pf2.gui.CircuitFrame;
import ch.usi.inf.pf2.gui.Model;
import ch.usi.inf.pf2.time.Schedule;
import ch.usi.inf.pf2.time.SetValueOfInputEvent;

public class SetInputTool extends Tool{

	private Input currentInput;
	private Value valueToSet;
	private CircuitFrame circuitFrame;
	
	public SetInputTool(final CircuitFrame circuitFrame, final Model model){
		super(model, "Select this tool and click on an input to switch the value.", "../icons/setInput.png", "../icons/setInputBg.png");
        currentInput = null;
        valueToSet = Value.UNKNOWN;
        this.circuitFrame = circuitFrame;
	}
	
	
	public void mouseClicked(final MouseEvent ev) {
		model.clearSelection();
		currentInput = null;
        final int x = ev.getX();
        final int y = ev.getY();
        final Component shape = getCircuit().findShapeAt(x, y);
        if (shape instanceof Input){
        	circuitFrame.getEditor().removeAll();
        	currentInput = ((Input) shape);
        	if(currentInput.getValue() == Value.TRUE){
        		valueToSet = Value.FALSE;
        		currentInput.setWaveform("0");
        	} else{
        		valueToSet = Value.TRUE;
        		currentInput.setWaveform("1");
        	}
        	
        	//currentInput.getLabel().setText(currentInput.getName() + ": " + currentInput.getWaveform());
        	currentInput.setLabel();
        	
        	for(Input input : model.getInputs()) {
        		circuitFrame.setEditor(input.getLabel());	
        	}
        	
        	circuitFrame.getEditor().revalidate();
        	

        	new SetValueOfInputEvent(valueToSet, currentInput, Schedule.getStep());
        	Schedule.stepRun();
        	model.shapeChanged();
        }
        else{
        }
	}
}
